﻿using System;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using TSharp.Core.AppBox.Areas.Admin.Models;
using TSharp.Core.Authentication.Facade;
using TSharp.Core.Mvc.Web;
namespace TSharp.Core.AppBox.Areas.Admin.Controllers
{
    public class UserController : TSharpController
    {
        //[Dependency]
        //public IAuthenticationService FormsService { get; set; }
        [Dependency]
        public IMembershipService svrmemb { get; set; }
        //
        // GET: /Admin/User/

        public ActionResult Index(int? id)
        {
            int index = id.HasValue ? id.Value : 1;

            if (index < 1)
                return this.RedirectToAction(m => m.Index(1));

            int totalRecords;
            var users = svrmemb.GetUsers(HttpContext, index - 1, PageSize, out totalRecords);
            var model = new UserList(users, index, PageSize, totalRecords);

            if (model.NeedRedirect)
            {
                return this.RedirectToAction(m => m.Index(model.TotalPageCount));
            }


            return View(model);
        }
        int PageSize = 10;
        [HttpPost]
        public ActionResult Index(int? id, FormCollection form)
        {
            int index = id.HasValue ? id.Value : 1;
            int totalRecords;
            var key = form["uname"];
            if (!string.IsNullOrEmpty(key))
            {
                TempData["uname"] = key;
                var model = svrmemb.FindByUserName(HttpContext, string.Format("%{0}%", key), 0, PageSize);

                return View(model);
            }
            else
            {
                var users = svrmemb.GetUsers(HttpContext, index - 1, PageSize, out totalRecords);
                var model = new UserList(users, index, PageSize, totalRecords);
                return View(model);
            }
        }
        //
        // GET: /Admin/User/Details/5

        public ActionResult Details(string id)
        {
            if (string.IsNullOrEmpty(id))
                return View();
            var user = svrmemb.GetUser(HttpContext, id, false);
            return PartialView("UserDetails", user);
        }


        //
        // GET: /Admin/User/Create

        public ActionResult Create()
        {
            //System.Threading.Thread.Sleep(1000);

            return PartialView("UserCreate", TSharp.Core.AppBox.Areas.Admin.Models.User.Default);
            // return View(TSharp.Core.AppBox.Areas.Admin.Models.User.Default);
        }

        //
        // POST: /Admin/User/Create

        [HttpPost]
        public ActionResult Create(TSharp.Core.AppBox.Areas.Admin.Models.User user)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    var UserName = user.UserName;
                    var Pwd = String.IsNullOrEmpty(user.Password) ? "123456" : user.Password;
                    var email = String.IsNullOrEmpty(user.Email) ? "tangjingbo@foxmail.com" : user.Email;
                    int n = 1;
                    if (Request.Form["isbatch"] == "1")
                        n = 5000;
                    int successCount = 0;
                    if (n == 1)
                    {
                        var status = svrmemb.CreateUser(UserName, Pwd, email);
                        if (status == System.Web.Security.MembershipCreateStatus.Success)
                            successCount++;
                    }
                    else
                        for (int i = 0; i < n; i++)
                        {
                            var status = svrmemb.CreateUser(UserName + "_" + i, Pwd, email);
                            if (status == System.Web.Security.MembershipCreateStatus.Success)
                                successCount++;
                        }
                    TempData["successCount"] = successCount;
                    return Success("创建成功", true);
                }
                catch
                {
                    return PartialView("UserCreate", user);
                }
            }
            return PartialView("UserCreate", user);
        }

        //
        // GET: /Admin/User/Edit/5

        public ActionResult Edit(int? id, string name)
        {
            var suser = svrmemb.GetUser(HttpContext, name, false);
            var user = new UserEdit()
            {
                UserName = suser.UserName,
                Email = suser.Email,
                BorthDay = suser.CreationDate

            };
            return View(user);
        }

        //
        // POST: /Admin/User/Edit/5

        [HttpPost]
        public ActionResult Edit(int? id, UserEdit user)
        {
            try
            {
                // TODO: Add update logic here
                //svrmemb.Update()
                


                return this.RedirectToAction(a => a.Index(id));
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/User/Delete/5


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public ActionResult Delete(int? page, string userName)
        {
            var user = svrmemb.GetUser(HttpContext, userName, false);
            return View(user);
        }

        //
        // POST: /Admin/User/Delete/5

        [HttpPost]
        public ActionResult Delete(int? page, FormCollection collection)
        {

            try
            {
                var username = collection.Get("UserName");

                svrmemb.DeleteUser(HttpContext, username);

                // TODO: Add delete logic here

                //return RedirectToAction("Index");
                return this.RedirectToAction(m => m.Index(page));
                //return RedirectToAction()
            }
            catch
            {
                return View();
            }
        }
    }
}
